package cn.iocoder.yudao.module.ride.service.ruledict;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ride.controller.admin.ruledict.vo.*;
import cn.iocoder.yudao.module.ride.dal.dataobject.ruledict.RuleDictDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ride.dal.mysql.ruledict.RuleDictMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.ride.enums.ErrorCodeConstants.RULE_DICT_NOT_EXISTS;
import static cn.iocoder.yudao.module.ride.enums.ErrorCodeConstants.*;

/**
 * 规则字典 Service 实现类
 *
 * @author tong
 */
@Service
@Validated
public class RuleDictServiceImpl implements RuleDictService {

    @Resource
    private RuleDictMapper ruleDictMapper;

    @Override
    public Integer createRuleDict(RuleDictSaveReqVO createReqVO) {
        // 插入
        RuleDictDO ruleDict = BeanUtils.toBean(createReqVO, RuleDictDO.class);
        ruleDictMapper.insert(ruleDict);

        // 返回
        return ruleDict.getId();
    }

    @Override
    public void updateRuleDict(RuleDictSaveReqVO updateReqVO) {
        // 校验存在
        validateRuleDictExists(updateReqVO.getId());
        // 更新
        RuleDictDO updateObj = BeanUtils.toBean(updateReqVO, RuleDictDO.class);
        ruleDictMapper.updateById(updateObj);
    }

    @Override
    public void deleteRuleDict(Integer id) {
        // 校验存在
        validateRuleDictExists(id);
        // 删除
        ruleDictMapper.deleteById(id);
    }

    @Override
        public void deleteRuleDictListByIds(List<Integer> ids) {
        // 删除
        ruleDictMapper.deleteByIds(ids);
        }


    private void validateRuleDictExists(Integer id) {
        if (ruleDictMapper.selectById(id) == null) {
            throw exception(RULE_DICT_NOT_EXISTS);
        }
    }

    @Override
    public RuleDictDO getRuleDict(Integer id) {
        return ruleDictMapper.selectById(id);
    }

    @Override
    public PageResult<RuleDictDO> getRuleDictPage(RuleDictPageReqVO pageReqVO) {
        return ruleDictMapper.selectPage(pageReqVO);
    }

}